<?php
  include "counter.php";
  if (!$area = $counter->get_input('area', 'area', 'get|post')) {
      $area = "All";
      $join = "?";
  } else {
      $join = "?area=$area&";
  }
  $query = mysql_query('SELECT * FROM places WHERE name=\''. $area .'\'');
  if (!$query) {
      print mysql_error();
      die('Query failed');
  }
  if (mysql_num_rows($query) > 0) {
      $place = mysql_fetch_array($query);
      $name = $place['name'];
      $longname = $place['longname'];
  }
  pagestart();
  pagehead('Area Rank Report for '. $name);
?>
<body>
<?php pagetop() ?>
<h1>
<img src="/gifs/Logo-small.png" alt="Linux Counter Logo">
Linux density for places in <?php echo $longname ?></h1>
This view gives you the ranking of density for places within <?php echo $longname ?> as of
<?php echo strftime("%a %b %d %H:%M:%S %Y %Z") ?>
<p>
You can click on each entry to get a list of places within that place.
<p>
To get population figures added, contact the maintainer for the area.<br>
The maintainer for this area is <?php echo findmaintainer($place) ?>
<p>
There are <?php echo $place['users'] ?> persons registered.
<p>
<?php
  if (!$orderby = $counter->get_input('orderby', 'alpha', 'get')) { $orderby = 'userdensity'; }
  if (!$limit = $counter->get_input('limit', 'number', 'get')) { $limit = 1; }
?>
<?php
  if ($orderby == 'longname') {
      $desc = 'asc';
  } else {
      $desc = 'desc';
  }
  $query = mysql_query('SELECT name, longname, users, population, IFNULL(users/population,0) AS userdensity FROM places WHERE within=\''. $place['f_key'] .'\' AND users >= '. $limit .' ORDER BY '. $orderby .' '. $desc);
  if ($query == 0) {
      print('</table>MySQL error: '. mysql_errno() .':'. mysql_error());
      die('Query for count failed');
  }
  $rank = 1;
  while ($arr = mysql_fetch_array($query)) {
      if ($rank == 1) {
?>
<a name="table"></a>
<h2>Where Linux users in <?php echo $longname ?> live</h2>
The table is sorted by
<?php
          if ($orderby == 'userdensity') {
              print 'number of users divided by population'."\n";
          } else {
              print $orderby;
          }
          if ($limit > 1) {
              print 'Places with less than '. $limit .' users are not listed'."\n";
          }
?>
<table border=4>
   <tr>
       <th>No</th>
       <th>ID</th>
       <!-- note - RFC 2396 specifies that fragment follows query part -->
       <th><a href="<?php echo $_SERVER['PHP_SELF'].$join ?>orderby=longname#table">Place</a></th>
       <th><a href="<?php echo $_SERVER['PHP_SELF'].$join ?>orderby=users#table">Pers</a></th>
       <th><a href="<?php echo $_SERVER['PHP_SELF'].$join ?>orderby=userdensity#table">P/Mpop</a></th>
       <th><a href="<?php echo $_SERVER['PHP_SELF'].$join ?>orderby=population#table">People</a></th>
   </tr>
<?
      }
?>
   <tr align=right>
       <td><?php echo $rank; ?></td>
       <td align=left><?php echo $arr['name']; ?></td>
       <td align=left><a href="<?php echo $_SERVER['PHP_SELF'] .'?area='. urlencode($arr['name']); if ($orderby != 'userdensity') { print '&orderby='. $orderby; } print '">'. $arr['longname'] .'</a>'; ?></td>
       <td><?php printf("%d", $arr['users']); ?></td>
       <td><?php if (isset($arr['userdensity'])) { printf("%5.2f", $arr['userdensity']); } else { print('Unknown'); } ?></td>
       <td><?php if ($arr['population'] >= 1) { printf("%6.1fM", $arr['population']); } elseif ($arr['population'] > 0) { printf("%6.0fK", $arr['population'] * 1000); } else { print('Unknown'); } ?></td>
   </tr>
<?php
      ++$rank;
  }
  if ($rank > 1) {
      echo '</table>';
  }
  pagebottom();
?>
</body>
</html>
